package com.zmx.product.web;

import com.zmx.common.common.web.AbstractController;
import com.zmx.common.common.web.rest.ResponseResult;
import com.zmx.product.dto.ProductDto;
import com.zmx.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: 商品控制器
 * @ClassName: ProductController
 * @Author zhaomxr
 * @Date 2022-01-19 15:03
 */
@Api("商品控制器")
@Slf4j
@RestController
@RequestMapping("test/product")
public class ProductController extends AbstractController<ProductDto, ProductService> {

    @Autowired
    private ProductService productService;

    @ApiOperation(value = "根据id查询商品信息", tags = "根据id查询商品信息")
    @GetMapping("getByProductId")
    public ResponseResult getByProductId(@RequestParam(required = true) Long productId) {
        try {
            ProductDto productDto = productService.findDtoById(productId);
            return ResponseResult.success(productDto);
        } catch (Exception e) {
            log.error("根据id查询商品信息失败 ", e);
            return ResponseResult.fail("根据id查询商品信息失败");
        }
    }

    @ApiOperation(value = "根据id查询商品信息", tags = "根据id查询商品信息")
    @GetMapping("getByProductIdIn")
    public ResponseResult getByProductIdIn(@RequestParam(required = true) Long productId) {
        try {
            Long[] productIds = new Long[]{productId};
            List<ProductDto> productDto = productService.findDtoByIdIn(productIds);
            return ResponseResult.success(productDto);
        } catch (Exception e) {
            log.error("根据id查询商品信息失败 ", e);
            return ResponseResult.fail("根据id查询商品信息失败");
        }
    }
}
